;layui.define([ "table", "form" ], function(exports) {
	tab = layui.table, form = layui.form;
	// 扩展自定义模块功能
	exports("dictionary", {
		//初始化分组////////////////////////////////////////////////
		initGroups : function(){
			var $ = layui.$;
		    $.ajax({
		  	  url: layui.setter.base+"admin/dictionary/parentJson?" + Math.random(),
		  	  dataType: "json",
		  	  type:"get",
		  	  success: function(res){
		  		var content = "<ul>";
		  		for(var i=0; i<res.length; i++){
		  			content = content + '<li id="'+res[i].id+'">';
		  			content = content + '<div class="group-body-left" code="'+res[i].code+'">';
		  			content = content + '<i class="layui-icon layui-icon-right"></i>';
		  			content = content + res[i].name;
		  			content = content + '</div>';
		  			content = content + '<span class="group-body-right">';
		  			content = content + "<span class='update' title='修改' id='"+res[i].id+"'><i class='layui-icon layui-icon-edit'></i></span>";
		  			content = content + "<span class='delete' title='删除' id='"+res[i].id+"'><i class='layui-icon layui-icon-delete'></i></span>";
		  			content = content + '</span>';
		  			content = content + '</li>';
		  		}
		  		var content = content + "</ul>";
		  		$("#groups .layui-card-body").html(content);
		  		
		  		// 绑定修改组
			    $("#groups .update").on('click', function(){
			    	var id = $(this).attr('id');
			    	parent.layer.open({
			          type: 2
			          ,title: '修改'
			          ,content: 'admin/dictionary/updatePage?id=' + id
			          ,maxmin: true
			          ,area: ['80%', '90%']
			          ,btn: ['确定', '取消']
			          ,yes: function(index, layero){
			             //点击确认触发 iframe 内容中的按钮提交
			             layero.find('iframe').contents().find("#layuiadmin-app-form-edit").click();
			           }
				    });
			    });
			    // 绑定删除组
			    $("#groups .delete").on('click', function(){
			    	var id = $(this).attr('id');
			    	layer.confirm("确定删除吗？",function(e){
						//请求删除接口
						layui.admin.req({
							url:layui.setter.base+"admin/dictionary/delete?id="+id,
							data: null,
							done: function(res){
								if(res.code !== 0){
						    		layer.msg(res.msg, {icon: 2});
								}else{
									layui.dictionary.initGroups();
									layer.close(e); //关闭
								}
							}
						});
					});
			    });
			    // 绑定展示子列表
			    $("#groups .group-body-left").on('click', function(){
			    	var code = $(this).attr('code');
			    	$("#children .header-right").attr("parentCode", code);
			    	tab.render({
			    		elem : "#LAY-web-list",
			    		url : layui.setter.base + "admin/dictionary/childrenJson?parentCode="+code,
			    		cols : [ [ {
			    			field : "name",
			    			title : "名称"
			    		}, {
			    			field : "code",
			    			title : "编码",
			    			sort : !0
			    		}, {
			    			title : "操作",
			    			width : 150,
			    			align : "center",
			    			fixed : "right",
			    			toolbar : "#table-content-list"
			    		} ] ],
			    		text: {none: '暂无相关数据'}
			    	});
			    });
			    // 绑定列表行操作
			    tab.on("tool(LAY-web-list)", function(row) {
					if ("update" === row.event) {
						parent.layer.open({
							type : 2,
							title : "编辑",
							content : layui.setter.base + "admin/dictionary/updatePage?id=" + row.data.id,
							maxmin : !0,
							area : [ '500px', '300px' ],
							btn : [ "确定", "取消" ],
							yes : function(index, layero) {
								// 点击确认触发 iframe 内容中的按钮提交
								layero.find('iframe').contents().find("#layuiadmin-app-form-edit").click();
							}
						});
						return;
					}
					
					if ("delete" === row.event) {
						layer.confirm("确定删除吗？", function(e) {
							console.log(row.data);
							// 请求删除接口
							layui.admin.req({
								url : layui.setter.base + "admin/dictionary/delete?id="+ row.data.id,
								data : null,
								done : function(res) {
									if (res.code !== 0) {
										layer.msg(res.msg, {icon : 2});
									} else {
										layer.msg(res.msg, {icon : 1});
										layui.table.reload('LAY-web-list'); // 重载表格
										layer.close(e); // 再执行关闭
									}
								}
							});
						});
						return;
					}
				});
		  	  },
		  	  error: function(xhr){parent.layer.msg("出错了" + xhr);}
		    });
		},
		//打开新增分组窗口////////////////////////////////////////////////
		setOpenAddGroup : function(){
			var $ = layui.$;
			$('#groups .header-right').on('click', function(){
				parent.layer.open({
		          type: 2
		          ,title: '添加'
		          ,content: 'admin/dictionary/addPage?parentCode=0'
		          ,maxmin: true
		          ,area: ['80%', '90%']
		          ,btn: ['确定', '取消']
		          ,yes: function(index, layero){
		             //点击确认触发 iframe 内容中的按钮提交
		             layero.find('iframe').contents().find("#layuiadmin-app-form-submit").click();
		           }
		        });
			});
		},
		//打开新增子项窗口////////////////////////////////////////////////
		setOpenAddItem : function(){
			var $ = layui.$;
			$('#children .header-right').on('click', function(){
				var code = $("#children .header-right").attr("parentCode");
				if(code == ""){
					layer.alert('需要先点击分组', {icon: 2});
					return;
				}
				parent.layer.open({
		          type: 2
		          ,title: '添加'
		          ,content: 'admin/dictionary/addPage?parentCode=' + code
		          ,maxmin: true
		          ,area: ['80%', '90%']
		          ,btn: ['确定', '取消']
		          ,yes: function(index, layero){
		             //点击确认触发 iframe 内容中的按钮提交
		             layero.find('iframe').contents().find("#layuiadmin-app-form-submit").click();
		           }
		        });
			});
		}
	})
});